

'game2d version of artemax world.
'adds I/O, bg color and entity template sets.

Type TGame2DWorld Extends TWorld

	Field _worldLabel:String
	Field _entityTemplateSets:TBag
	Field _worldR:Int
	Field _worldG:Int
	Field _worldB:Int

	
	
	Method New()
		_worldLabel = "My World"
		_entityTemplateSets = New TBag
	End Method
	
	
	Method GetWorldLabel:String()
		Return _worldLabel
	End Method
	
	
	Method SetWorldLabel(newLabel:String)
		_worldLabel = newLabel
	End Method

		
	Method GetTemplateSets:TBag()
		Return _entityTemplateSets
	End Method
	
	Method GetRed:Int()
		Return _worldR
	End Method
	
	Method GetGreen:Int()
		Return _worldG
	End Method

	Method GetBlue:Int()
		Return _worldB
	End Method
	
	Method SetRed(r:Int)
		_worldR = r
	End Method
	
	Method SetGreen(g:Int)
		_worldG = g
	End Method

	Method SetBlue(b:Int)
		_worldB = b
	End Method
	
	
	'read template sets from world file
	Method ReadTemplateSets(s:TStream, rootFolder:String)
		Local newLine:String = ReadLine(s)
		While newLine <> "[/templatesets]"
			Local vals:String[] = newLine.Split("=")
			If vals[0] = "set"
				'0 = filename, 1 = index of set
				Local settings:String[] = vals[1].Split(",")
				Local newSet:TEntityTemplateSet = New TEntityTemplateSet
				
				'try to open a stream to the set file
				Local newStream:TStream = ReadStream(rootFolder + settings[0])
				If newStream
					newSet.Read(newStream, Int(settings[1]))
					newSet.SetFilename(settings[0])
					CloseStream(newStream)
					_entityTemplateSets.Set(Int(settings[1]), newSet)
				Else
					RuntimeError("cannot open set: " + rootFolder + settings[0])
				End If
			EndIf
			newLine = ReadLine(s)
		Wend
	End Method

	
	
	'writes out the filenames of loaded sets and their index to world file.
	Method WriteTemplateSets(s:TStream)
		WriteLine(s, "[templatesets]")
		For Local index:Int = 0 To _entityTemplateSets.GetSize() - 1
			Local tempSet:TEntityTemplateSet = TEntityTemplateSet(_entityTemplateSets.Get(index))
			WriteLine(s, "set=" + StripDir(tempSet.GetFilename()) + "," + index)
		Next
		WriteLine(s, "[/templatesets]")
	End Method
	

	
	Method ReadWorldSettings(s:TStream)
		Local newLine:String = ReadLine(s)
		While newLine <> "[/settings]"
			Local vals:String[] = newLine.Split("=")
			Select vals[0]
				Case "mapname"
					_worldLabel = vals[1]
				Case "bgcolor"
					Local colors:String[] = vals[1].Split(",")
					_worldR = Int(colors[0])
					_worldG = Int(colors[1])
					_worldB = Int(colors[2])
				Default
					RuntimeError "worldsettings: " + newLine
			End Select
			newLine = ReadLine(s)
		Wend
	End Method	
		
	
	Method WriteWorldSettings(s:TStream)
		WriteLine(s, "[settings]")
		WriteLine(s, "mapname=" + _worldLabel)
		WriteLine(s, "bgcolor=" + _worldR + "," + _worldG + "," + _worldB)
		WriteLine(s, "[/settings]")
	End Method

End Type
